{% extends 'layout/basic.html' %}
{% load static %}
{% block title %} 用户登录 {% endblock %}
{% block css %}
    <link rel="stylesheet" href="{% static 'css/account.css' %}">
    <style>
        .error-msg {
            color: red;
            position: absolute;
        }
    </style>
{% endblock %}

{% block content %}
    <div class="account">
        <div class="title">用户登录</div>
        <form id="loginForm" method="post" novalidate>
            {% csrf_token %}
            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        <div class="clearfix">
                            <div class="col-md-7" style="padding-left: 0">
                                {{ field }}
                                <span class="error-msg">{{ field.errors.0 }}</span>
                            </div>
                            <div class="col-md-5">
                                <img src="{% url 'image_code' %}" id="imgCode" alt="xxx" title="点击更换图片"/>
                            </div>
                        </div>
                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {{ field }}
                        <span class="error-msg">{{ field.errors.0 }}</span>
                    </div>
                {% endif %}
            {% endfor %}
            <div>
                <div style="float: right;">
                    <a href="{% url 'login_email' %}">邮箱验证码登录?</a>
                </div>
            </div>
            <input id="btnSubmit" type="button" class="btn btn-primary" value="登 录" style="width: 100%"></input>
        </form>
    </div>
{% endblock %}

{% block js %}
    <script>
        // 页面框架加载完成之后自动会执行的代码
        $(function () {
            bindClickSubmit();
            bindChangeImageCode();
        });

        /*
        点击提交（登录）
        */
        function bindClickSubmit() {
            $('#btnSubmit').click(function () {
                $('.error-msg').empty();
                axios.post("{% url 'login' %}", $('#loginForm').serialize() // 所有字段数据 + csrf token
                ).then(res => {
                    console.log(res);
                    if (res.data.status) {
                        // 跳转到指定路径
                        location.href = res.data.next;
                    } else {
                        $.each(res.data.error, function (key, value) {
                            $("#id_" + key).next().text(value[0]);
                        })

                        // 再刷新一下图片验证码
                        $('#imgCode').click(function () {
                            let oldSrc = $(this).attr('src');
                            $(this).attr('src', oldSrc + '?');  // 换了个地址就会再次发送请求
                        })
                    }
                }, err => {
                    console.log(err)
                })
            })
        }

        function bindChangeImageCode() {
            $('#imgCode').click(function () {
                let oldSrc = $(this).attr('src');
                $(this).attr('src', oldSrc + '?');  // 换了个地址就会再次发送请求
            })
        }
    </script>
{% endblock %}